<template>
  <div class="dashboard-container">
    <div class="app-container">
      <h2>设置</h2>
      <el-tabs v-model="activeName">
        <el-tab-pane label="角色管理" name="first">
          <el-row style="height: 60px">
            <el-button icon="el-icon-plus" size="small" type="primary" @click="showDialog = true">新增角色</el-button>
          </el-row>
          <!-- 表格 -->
          <el-table :data="list" border>
            <el-table-column label="序号" type="index" width="120" />
            <el-table-column prop="name" label="角色名称" width="240" />
            <el-table-column prop="description" label="描述" />
            <el-table-column label="操作">
              <!-- <template slot-scope="scope">
                <el-button size="small" type="success">分配权限</el-button>
                <el-button size="small" type="primary">编辑</el-button>
                <el-button size="small" type="danger" @click="del(scope.row.id)">删除</el-button>
              </template> -->
              <template v-slot:default="slotProps">
                <el-button size="small" type="success" @click="assignPermiss(slotProps.row.id)">分配权限</el-button>
                <el-button size="small" type="primary" @click="edit(slotProps.row)">编辑</el-button>
                <el-button size="small" type="danger" @click="del(slotProps.row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            :current-page="page.page"
            :page-sizes="[4, 8, 12, 16]"
            :page-size="page.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-tab-pane>
        <el-tab-pane label="公司信息" name="second">
          <el-form label-width="120px" style="margin-top: 50px">
            <el-form-item label="公司名称">
              <el-input v-model="formData.name" disabled style="width: 400px" />
            </el-form-item>
            <el-form-item label="公司地址">
              <el-input v-model="formData.companyAddress" disabled style="width: 400px" />
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input v-model="formData.mailbox" disabled style="width: 400px" />
            </el-form-item>
            <el-form-item label="备注">
              <el-input v-model="formData.remarks" type="textarea" :rows="3" disabled style="width: 400px" />
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>

      <el-dialog :title="showTitle" :visible.sync="showDialog" @close="clearForm">
        <el-form ref="roleForm" :model="roleForm">
          <el-form-item label="角色名称" :label-width="formLabelWidth">
            <el-input v-model="roleForm.name" autocomplete="off" />
          </el-form-item>
          <el-form-item label="角色描述" :label-width="formLabelWidth">
            <el-input v-model="roleForm.description" autocomplete="off" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="showDialog = false">取 消</el-button>
          <el-button type="primary" @click="updataRole">确 定</el-button>
        </div>
      </el-dialog>

      <el-dialog title="分配权限" :visible="showPermDialog" @close="btnPermCancel">
        <!-- 确定 取消 -->
        <el-tree ref="permTree" :data="permData" :props="defaultProps" :show-checkbox="true" :check-strictly="true" :default-expand-all="true" :default-checked-keys="selectCheck" node-key="id" />

        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button type="primary" size="small" @click="btnPermOK">确定</el-button>
            <el-button size="small" @click="btnPermCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRoleListApi, getCompanyInfoApi, deleteRoleByIdApi, updateRoleApi, addRoleApi } from '@/api/setting'
import { mapGetters } from 'vuex'
import { tranListToTreeData } from '@/utils'
import { getPermissionList, getRoleDetail, updateRolePermiss } from '@/api/permisson'
export default {
  name: 'Setting',
  components: {},
  data() {
    return {
      activeName: 'first',
      page: {
        // 放置页码及相关数据
        page: 1,
        pagesize: 4,
        total: 0, // 记录总数
      },
      list: [],
      formData: {},
      showDialog: false,
      roleForm: {},
      formLabelWidth: '120',
      showPermDialog: false,
      permData: [], // 权限的树形结构
      defaultProps: {
        label: 'name',
      },
      selectCheck: [], // 选中的节点
      roleId: '',
    }
  },
  computed: {
    ...mapGetters(['companyId']),
    showTitle() {
      return this.roleForm.id ? '编辑角色' : '新增角色'
    },
  },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  mounted() {},
  methods: {
    async getRoleList() {
      const { total, rows } = await getRoleListApi(this.page)
      this.page.total = total
      this.list = rows
    },
    async getCompanyInfo() {
      // 从vuex取值
      // console.log('第一种 companyid:', this.$store.state.user.userInfo.companyId)
      // console.log('第二种 getters:', this.$store.getters.companyId)
      this.formData = await getCompanyInfoApi(this.companyId)
    },
    async del(id) {
      try {
        await this.$confirm('确定删除吗')
        await deleteRoleByIdApi(id)
        this.getRoleList()
        this.$message.success('删除成功')
      } catch (error) {
        console.log('error')
      }
    },
    edit(row) {
      this.showDialog = true
      this.roleForm = { ...row }
    },
    async updataRole() {
      if (this.roleForm.id) {
        await updateRoleApi(this.roleForm)
      } else {
        await addRoleApi(this.roleForm)
      }

      this.showDialog = false
      this.getRoleList()
      this.$message.success('操作成功')
    },
    clearForm() {
      this.roleForm = {}
    },
    handleSizeChange(val) {
      this.page.pagesize = val
      this.getRoleList()
    },
    handleCurrentChange(val) {
      this.page.page = val
      this.getRoleList()
    },
    btnPermCancel() {
      this.showPermDialog = false
    },
    async btnPermOK() {
      await updateRolePermiss({ id: this.roleId, permIds: this.selectCheck })
      this.showPermDialog = false
    },
    async assignPermiss(id) {
      this.roleId = id
      this.permData = tranListToTreeData(await getPermissionList(), '0') // 转化list到树形数据
      const { permIds } = await getRoleDetail(id) // permIds是当前角色所拥有的权限点数据
      this.selectCheck = permIds // 将当前角色所拥有的权限id赋值
      this.showPermDialog = true
    },
  },
}
</script>

<style scoped lang="scss">
.el-table--border,
.el-table--group {
  border: 1px solid pink;
}
</style>
